<?php
namespace BP\En;
require_once("java/util/ArrayList.php");
require_once("java/util/Collections.php");
require_once("java/util/Comparator.php");
require_once("java/util/Hashtable.php");
require_once("java/util/Iterator.php");
require_once("java/util/Set.php");
require_once("BP/DA/DataType.php");
require_once("BP/DA/Log.php");
require_once("BP/Sys/EventBase.php");
require_once("BP/Sys/GEDtl.php");
require_once("BP/Sys/GEEntity.php");
require_once("BP/Sys/SystemConfig.php");
require_once("BP/Tools/ClassUtils.php");
require_once("BP/Tools/StringUtils.php");
class ClassFactory {
	public static $Htable_Evbase;	// Hashtable
	protected static $objects;	// Hashtable
	protected static $Htable_En;	// Hashtable<String, Object>
	protected static $Htable_Method;	// Hashtable<String, Object>
	public static $Htable_Ens;	// Hashtable<String, Object>
	public static $Htable_XmlEns;	// Hashtable<String, Object>
	public static $Htable_XmlEn;	// Hashtable<String, Object>
	protected static $Htable_HandlerPage;	// Hashtable
	public static function __staticinit() { // static class members
		self::$objects = new Hashtable();
	}
	public static function GetEventBase ($className) // [String className]
	{
		if (((self::$Htable_Evbase == NULL) || self::$Htable_Evbase->isEmpty()))
		{
			self::$Htable_Evbase = new Hashtable();
			$cl = "BP.Sys.EventBase";
			$al = ClassFactory::GetObjects($cl);
			self::$Htable_Evbase->clear();
			foreach ($al as $en) 			{
				try 
				{
					self::$Htable_Evbase->put(($en)->getClass()->getName(), $en);
				}
				catch (\Exception $e)
				{ /* empty */ }
			}
		}
		$ens = (( ((self::$Htable_Evbase->get($className) instanceof EventBase)) ? self::$Htable_Evbase->get($className) : NULL ));
		return $ens;
	}
	public static function GetObject_OK ($className) // [String className]
	{
		if (($DataType->IsNullOrEmpty($className) ==  TRUE ))
			return "err@要转化类名称为空...";
		$clazz = $Class->forName($className);
		return $clazz->newInstance();
	}
	public static function GetObjects ($baseEnsName) // [String baseEnsName]
	{
		$list = self::$objects->get($baseEnsName);
		if (($list != NULL))
		{
			return $list;
		}
		try 
		{
			$list = [];// 
			$parent = $Class->forName($baseEnsName);
			$set = $ClassUtils->findImplementations($parent, "BP");
			for ($it = $set->iterator(); $it->hasNext(); ) 
			{
				$clazz = $it->next();
				try 
				{
					$list->add($clazz->newInstance());
				}
				catch (InstantiationException $e)
				{
					$e->printStackTrace();
				}
				catch (IllegalAccessException $e)
				{
					$e->printStackTrace();
				}
			}
			$Collections->sort($list, new Comparator());
			$Log->DebugWriteInfo(((((("扫描 " . $baseEnsName) . " 父类，共 ") . $set->size()) . " 子类�?") . $set));
			self::$objects->put($baseEnsName, $list);
			return $list;
		}
		catch (ClassNotFoundException $e1)
		{
			$e1->printStackTrace();
		}
		return NULL;
	}
	public static function GetEn ($className) // [String className]
	{
		if (($className->contains(".") ==  FALSE ))
		{
			if (($className->contains("Dtl") ==  TRUE ))
			{
				return new GEDtl($className);
			}
			else
			{
				return new GEEntity($className);
			}
		}
		if ((self::$Htable_En == NULL))
		{
			self::$Htable_En = new Hashtable();
			$cl = "BP.En.Entity";
			$al = ClassFactory::GetObjects($cl);
			foreach ($al as $en) 			{
				if (((NULL == $en) || $StringUtils->isEmpty($en->getClass()->getName())))
					continue;
				if ((self::$Htable_En->containsKey($en->getClass()->getName()) ==  FALSE ))
				{
					self::$Htable_En->put($en->getClass()->getName(), $en);
				}
				else
				{
					continue;
				}
			}
		}
		$tmp = self::$Htable_En->get($className);
		if (($tmp == NULL))
			return NULL;
		$en = $tmp;
		$en->setRow(NULL);
		return $en;
	}
	public static function GetMethod ($className) // [String className]
	{
		if ((self::$Htable_Method == NULL))
		{
			self::$Htable_Method = new Hashtable();
			$cl = "BP.En.Method";
			$al = ClassFactory::GetObjects($cl);
			foreach ($al as $en) 			{
				self::$Htable_Method->put($en->getClass()->getName(), $en);
			}
		}
		$tmp = self::$Htable_Method->get($className);
		return ((( (($tmp instanceof BP.En.Method)) ? $tmp : NULL )));
	}
	public static function GetEns ($className) // [String className]
	{
		if (($className->contains(".") ==  FALSE ))
		{
			$myens = new GEEntitys($className);
			return $myens;
		}
		self::$Htable_Ens = new Hashtable();
		$cl = "BP.En.Entities";
		$al = ClassFactory::GetObjects($cl);
		self::$Htable_Ens->clear();
		foreach ($al as $en) 		{
			try 
			{
				self::$Htable_Ens->put($en->getClass()->getName(), $en);
			}
			catch (\Exception $e)
			{ /* empty */ }
		}
		$ens = (( ((self::$Htable_Ens->get($className) instanceof Entities)) ? self::$Htable_Ens->get($className) : NULL ));
		return $ens;
	}
	public static function GetEnsNew ($className) // [String className]
	{
		if (($className->contains(".") ==  FALSE ))
		{
			$myens = new GEEntitys($className);
			return $myens;
		}
		try 
		{
			$catClass = $Class->forName($className);
			$obj = $catClass->newInstance();
			return $obj;
		}
		catch (Exception $e1)
		{
			$e1->printStackTrace();
			if ((1 == 1))
				return NULL;
		}
		self::$Htable_Ens = new Hashtable();
		$cl = "BP.En.Entities";
		$al = ClassFactory::GetObjects($cl);
		self::$Htable_Ens->clear();
		foreach ($al as $en) 		{
			try 
			{
				self::$Htable_Ens->put($en->getClass()->getName(), $en);
			}
			catch (\Exception $e)
			{ /* empty */ }
		}
		$ens = (( ((self::$Htable_Ens->get($className) instanceof Entities)) ? self::$Htable_Ens->get($className) : NULL ));
		return $ens;
	}
	public static function GetXmlEns ($className) // [String className]
	{
		if ((self::$Htable_XmlEns == NULL))
		{
			self::$Htable_XmlEns = new Hashtable();
			$cl = "BP.XML.XmlEns";
			$al = ClassFactory::GetObjects($cl);
			foreach ($al as $en) 			{
				self::$Htable_XmlEns->put($en->getClass()->getName(), $en);
			}
		}
		$tmp = self::$Htable_XmlEns->get($className);
		return ((( (($tmp instanceof BP.Sys.XML.XmlEns)) ? $tmp : NULL )));
	}
	public static function GetXmlEn ($className) // [String className]
	{
		if ((self::$Htable_XmlEn == NULL))
		{
			self::$Htable_XmlEn = new Hashtable();
			$cl = "BP.XML.XmlEn";
			$al = ClassFactory::GetObjects($cl);
			foreach ($al as $en) 			{
				self::$Htable_XmlEn->put($en->getClass()->getName(), $en);
			}
		}
		$tmp = self::$Htable_XmlEn->get($className);
		return ((( (($tmp instanceof BP.Sys.XML.XmlEn)) ? $tmp : NULL )));
	}
	public static function getClassPath () 
	{
		$path = $Thread->currentThread()->getContextClassLoader()->getResource("");
		$path = $path->substring(($path->indexOf("/") + 1));
		$i = 0;
		while ((($path->indexOf("/") != -1) && ($path->length() > 0))) 
		{
			$path = $path->substring(0, $path->lastIndexOf("/"));
			if ((1 == $i))
			{
				$service = $SystemConfig->getAppSettings()->get("Service")->toLowerCase();
				if ($service->equals("tomcat"))
				{
					$path += (($System->getProperty("file.separator") . "lib") . $System->getProperty("file.separator"));
					return $path;
				}
				else
					if ($service->equals("jetty"))
					{
						$path += $System->getProperty("file.separator");
						return $path;
					}
			}
			++$i;
		}
		return $path->substring(($path->indexOf("/") + 1), ($path->lastIndexOf("/") - 7));
	}
	public static function GetHandlerPage ($className) // [String className]
	{
		if ((self::$Htable_HandlerPage == NULL))
		{
			self::$Htable_HandlerPage = new Hashtable();
			$cl = "BP.WF.HttpHandler.DirectoryPageBase";
			$al = ClassFactory::GetObjects($cl);
			foreach ($al as $en) 			{
				$key = "";
				if (((NULL == $en) || $DataType->IsNullOrEmpty($key = $en)))
					continue;
				if ((self::$Htable_HandlerPage->containsKey($key) ==  FALSE ))
					self::$Htable_HandlerPage->put($key, $en);
			}
		}
		return self::$Htable_HandlerPage->get($className);
	}
}
ClassFactory::__staticinit(); // initialize static vars for this class on load
?>
